﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ecommerce2011.Models;

namespace ecommerce2011.Controllers
{
    public class CartItemController : Controller
    {
        private ProductosEcommerceEntities db = new ProductosEcommerceEntities();
        //
        // GET: /CartItem/

        public ViewResult Index()
        {
            var carrito = (List<CartItem>)Session["carrito"];
            Decimal precioUnitario = 0;
            Decimal totalCarrito = 0;
            Product product;
            if (carrito == null)
                carrito = new List<CartItem>();
            foreach (var item in carrito)
            {
                precioUnitario = 0;
                product = db.Product.Find(item.ProductId);
                if (product.ShowRate != null)
                    precioUnitario += product.ShowRate.Price;
                if (product.City.CityRate != null)
                    precioUnitario += product.City.CityRate.Price;
                if (product.TransportationRate != null)
                    precioUnitario += product.TransportationRate.Price;
                if (product.AccommodationRate != null)
                    precioUnitario += product.AccommodationRate.Price;
                item.UnitPrice = precioUnitario;
                item.TotalPrice = precioUnitario * item.Quantity;
                totalCarrito += (precioUnitario * item.Quantity);
            }
            ViewData["TotalOrden"] = totalCarrito;
            return View(carrito);
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(long id)
        {
            var carrito = (List<CartItem>)Session["carrito"];
            CartItem item = carrito.Find(c => c.ProductId == id);
            return View(item);
        }

        //
        // POST: /CartItem/Edit/5

        [HttpPost]
        public ActionResult Edit(CartItem itemCarrito)
        {
            if (ModelState.IsValid)
            {
                var carrito = (List<CartItem>)Session["carrito"];
                if (carrito == null)
                    carrito = new List<CartItem>();
                if (itemCarrito.Quantity == 0)
                    carrito.RemoveAll(c => c.ProductId == itemCarrito.ProductId);
                else
                    carrito.Find(c => c.ProductId == itemCarrito.ProductId).Quantity = itemCarrito.Quantity;
                return RedirectToAction("Index");
            }
            return View(itemCarrito);
        }

        //
        // GET: /Product/Edit/5
        [Authorize]
        [HttpPost]
        public ActionResult ConfirmProcessOrder()
        {
            var carrito = (List<CartItem>)Session["carrito"];
            Decimal precioUnitario = 0;
            Decimal totalCarrito = 0;
            Product product;
            if (carrito == null)
                carrito = new List<CartItem>();
            foreach (var item in carrito)
            {
                precioUnitario = 0;
                product = db.Product.Find(item.ProductId);
                if (product.ShowRate != null)
                    precioUnitario += product.ShowRate.Price;
                if (product.City.CityRate != null)
                    precioUnitario += product.City.CityRate.Price;
                if (product.TransportationRate != null)
                    precioUnitario += product.TransportationRate.Price;
                if (product.AccommodationRate != null)
                    precioUnitario += product.AccommodationRate.Price;
                item.UnitPrice = precioUnitario;
                item.TotalPrice = precioUnitario * item.Quantity;
                totalCarrito += (precioUnitario * item.Quantity);
            }
            ViewData["TotalOrden"] = totalCarrito;
            //Dummy
            //servicioAutenticador.customer cliente = new servicioAutenticador.servicioAutenticadorService().consultarCliente(System.Web.HttpContext.Current.User.Identity.Name);
            //OSB
            ClientesServiceProxy.wsClientesService srv = new ClientesServiceProxy.wsClientesService();
            ClientesServiceProxy.customerDTO[] cliente = srv.buscarClienteID(System.Web.HttpContext.Current.User.Identity.Name);

            if (cliente[0].creditcardnumber == null)
                ViewData["TieneTarjeta"] = (bool)false;
            else
                ViewData["TieneTarjeta"] = (bool)true;
            return View();
        }

        //
        // POST: /CartItem/ProcessOrder/5
        [Authorize]
        [HttpPost]
        public ActionResult ProcessOrder(string creditCardType, string creditCardNumber)
        {
            var carrito = (List<CartItem>)Session["carrito"];
            int numItems = carrito.Count;
            string numeroOrden;
            Decimal precioUnitario = 0;
            Decimal totalCarrito = 0;
            Product product;
            if (carrito == null)
                carrito = new List<CartItem>();
            //Patricia
            //OrdenesService.wsOrdenesService crearOrden = new OrdenesService.wsOrdenesService();
            //OrdenesService.ordersDTO[] ordenes = new OrdenesService.ordersDTO[1];
            //OrdenesService.customerDTO cliente = new OrdenesService.customerDTO();
            //cliente.custid = System.Web.HttpContext.Current.User.Identity.Name;
            //ordenes[0] = new OrdenesService.ordersDTO();
            //ordenes[0].customer = cliente;
            //OrdenesService.itemsDTO[] itemsOrden = new OrdenesService.itemsDTO[numItems];
            //OSB
            CrearOrdenProxy.WsCrearOrdenesService crearOrden = new CrearOrdenProxy.WsCrearOrdenesService();
            CrearOrdenProxy.ordersDTO ordenes = new CrearOrdenProxy.ordersDTO();
            CrearOrdenProxy.customerDTO cliente = new CrearOrdenProxy.customerDTO();
            cliente.custid = System.Web.HttpContext.Current.User.Identity.Name;
            ordenes = new CrearOrdenProxy.ordersDTO();
            ordenes.customer = cliente;
            CrearOrdenProxy.itemsDTO[] itemsOrden = new CrearOrdenProxy.itemsDTO[numItems];

            //Dummy
            //servicioAutenticador.customer cliente = new servicioAutenticador.servicioAutenticadorService().consultarCliente(System.Web.HttpContext.Current.User.Identity.Name);
            //OSB
            ClientesServiceProxy.customerDTO[] clienteConsulta = new ClientesServiceProxy.wsClientesService().buscarClienteID(System.Web.HttpContext.Current.User.Identity.Name);
            if (clienteConsulta[0].creditcardnumber == null)
            {
                //if(creditCardNumber.HasValue)
                ordenes.customer.creditcardnumber = creditCardNumber;
                //if (creditCardType.HasValue)
                ordenes.customer.creditcardtype = creditCardNumber;
            }else{
                ordenes.customer.creditcardnumber = clienteConsulta[0].creditcardnumber;
                ordenes.customer.creditcardtype = clienteConsulta[0].creditcardtype;
            }
            ordenes.customer.addressCollection = null;
            ordenes.customer.email = clienteConsulta[0].email;
            ordenes.customer.fname = clienteConsulta[0].fname;
            ordenes.customer.lname = clienteConsulta[0].lname;
            ordenes.customer.password = clienteConsulta[0].password;
            ordenes.customer.phonenumber = clienteConsulta[0].phonenumber;
            ordenes.customer.status = clienteConsulta[0].status;
            int itemNo = 0;
            foreach (var item in carrito)
            {
                precioUnitario = 0;
                product = db.Product.Find(item.ProductId);
                if (product.ShowRate != null)
                    precioUnitario += product.ShowRate.Price;
                if (product.City.CityRate != null)
                    precioUnitario += product.City.CityRate.Price;
                if (product.TransportationRate != null)
                    precioUnitario += product.TransportationRate.Price;
                if (product.AccommodationRate != null)
                    precioUnitario += product.AccommodationRate.Price;
                item.UnitPrice = precioUnitario;
                //OSB
                itemsOrden[itemNo] = new CrearOrdenProxy.itemsDTO();
                itemsOrden[itemNo].price = precioUnitario;
                itemsOrden[itemNo].priceSpecified = true;
                itemsOrden[itemNo].prodid = product.ProductId;
                itemsOrden[itemNo].productid = product.Name;
                itemsOrden[itemNo].quantity = item.Quantity;
                itemNo++;
            }
            ordenes.itemsCollection = itemsOrden;
            numeroOrden = crearOrden.create(ordenes);
            Session["carrito"] = null;
            ViewData["NumeroOrden"] = numeroOrden;
            return View(carrito);
        }
    }
}
